package resource.manager.classes;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import android.util.Log;

public class RuntimeManager {

	public void getCpuUsage(){
		
	}
	
	public String execCommand(String command){
		String result = "";
		try{
            Process process;            
            process = Runtime.getRuntime().exec("top -n 1 -d 1");
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String buffer;
            while ((buffer = in.readLine()) != null) {
            result = result + "\n" + buffer;
            }
	    }
	    catch (Exception e) 
	    {
	    	e.printStackTrace();
	    }
	    ArrayList<HashMap<String, Object>> test = getArrayList(result);
	    for (HashMap<String, Object> tes: test){
	    	
	    }
	    return result;
	}
	
	public ArrayList<HashMap<String, Object>> getArrayList(String result){
		ArrayList<HashMap<String, Object>> resultset = new ArrayList<HashMap<String, Object>>();
		Log.i("topresult", result);
		String[] datas = result.split("\n");
		Log.i("datas.length", datas.length + "");
		for (int i = 1; i< datas.length; i++){
			HashMap<String, Object> application = new HashMap<String, Object>();
			String[] data = datas[i].split(" ");
			Log.i("data.length", data.length + "");
			Log.i("package_pid", datas[i] + "");
			resultset.add(application);
		}

		Log.i("!!!!!!!", resultset.size()+"");
		return resultset;
	}
}

